home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / graphics / qrt.lzh / NORM.C < prev    next >
C/C++ Source or Header  |  1989-02-16  |  3KB  |  112 lines

  1.  
  2. /**********************************************************
  3.  
  4.    ** Normal finder ** - one proceedure here for each
  5.    primitive type.  Functions are pointed at by entries
  6.    in the ObjData structure. Some functions will find
  7.    the normal for >1 object.
  8.  
  9.  **********************************************************/
  10.  
  11. #include "qrt.h"
  12.  
  13. /**********************************************************
  14.  
  15.            Generates normal to sphere ( or lamp )
  16.  
  17.  **********************************************************/
  18.  
  19. SphereNorm(norm,object,position)
  20.   VECT_PTR norm, position;
  21.   OBJ_PTR object;
  22. {
  23. # ifdef ROBUST
  24.     if (!((object->type == SPHERE) ||
  25.           (object->type == LAMP)))
  26.       Error(INTERNAL_ERROR,501);
  27. # endif
  28.  
  29.   VecSubtract(norm,position,&(object->loc));
  30.   Normalize(norm);
  31. }
  32.  
  33.  
  34. /**********************************************************
  35.  
  36.              Generates normal to planar object
  37.  
  38.  **********************************************************/
  39.  
  40. PlaneNorm(norm,object,position)
  41.   VECT_PTR norm, position;
  42.   OBJ_PTR object;
  43. {
  44. # ifdef ROBUST
  45.     if (!((object->type == PARALLELOGRAM) ||
  46.           (object->type == RING)          ||
  47.           (object->type == TRIANGLE)))
  48.       Error(INTERNAL_ERROR,502);
  49. # endif
  50.  
  51.   VectEQ(norm,&(object->precomp.norm));
  52. }
  53.  
  54.  
  55. /**********************************************************
  56.  
  57.           Generates normal to quadratic surface
  58.                     ^         ^         ^
  59.   Normal to { a*x*x i + b*y*y j + c*z*z k = d } is:
  60.                     ^         ^         ^
  61.             { 2*a*x i + 2*b*y j + 2*c*d k }
  62.  
  63.  **********************************************************/
  64.  
  65. QuadraticNorm(norm,object,position)
  66.   VECT_PTR norm, position;
  67.   OBJ_PTR object;
  68. {
  69.   VECTOR newpos, newdir;
  70.  
  71. # ifdef ROBUST
  72.     if (object->type != QUADRATIC)
  73.       Error(INTERNAL_ERROR,503);
  74. # endif
  75.  
  76.   /* translate collision point */
  77.  
  78.   VecSubtract(&newpos,position,&(object->loc));
  79.  
  80.   if ((object->vect1.x == 0) &&             /* no rotation  */
  81.       (object->vect1.y == 1) &&             /* if aligned   */
  82.       (object->vect1.z == 0))   {
  83.  
  84.       norm->x = object->vect2.x * newpos.x; /* throw out factor of */
  85.       norm->y = object->vect2.y * newpos.y; /* 2 because of normalization */
  86.       norm->z = object->vect2.z * newpos.z;
  87.  
  88.       Normalize(norm);
  89.  
  90.   } else {                                  /* here we must rotate */
  91.  
  92.     Rot12( &newpos, &newpos,                /* rotate position */
  93.            object->precomp.cos1,
  94.            object->precomp.sin1,
  95.            object->precomp.cos2,
  96.            object->precomp.sin2 );
  97.  
  98.     newdir.x = object->vect2.x * newpos.x;  /* normal */
  99.     newdir.y = object->vect2.y * newpos.y;
  100.     newdir.z = object->vect2.z * newpos.z;
  101.  
  102.     Rot21( &newdir, norm,                    /* rotate back */
  103.            object->precomp.cos1,
  104.            object->precomp.sin1,
  105.            object->precomp.cos2,
  106.            object->precomp.sin2 );
  107.  
  108.   }
  109.  
  110. }
  111.  
  112.